﻿namespace Smart.ERP
{
    using DevExpress.XtraEditors;
    using Smart.Common;
    using Smart.Data.Helper;
    using Smart.Utils.Lib;
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Windows.Forms;

    public class PRODUCT_UNIT
    {
        private string m_Product_ID;
        private long m_Sorted;
        private string m_Unit_ID;
        private decimal m_UnitConvert;
        private string m_UnitConvert_ID;

        public PRODUCT_UNIT()
        {
            this.m_Product_ID = "";
            this.m_Unit_ID = "";
            this.m_UnitConvert_ID = "";
            this.m_UnitConvert = 0M;
            this.m_Sorted = 0L;
        }

        public PRODUCT_UNIT(string Product_ID, string Unit_ID, string UnitConvert_ID, decimal UnitConvert, long Sorted)
        {
            this.m_Product_ID = Product_ID;
            this.m_Unit_ID = Unit_ID;
            this.m_UnitConvert_ID = UnitConvert_ID;
            this.m_UnitConvert = UnitConvert;
            this.m_Sorted = Sorted;
        }

        public void AddCombo(System.Windows.Forms.ComboBox combo)
        {
            this.AddCombo(combo, this.LookUp());
        }

        public void AddCombo(System.Windows.Forms.ComboBox combo, DataTable dt)
        {
            MyLib.TableToComboBox(combo, dt, "ID", "Name");
        }

        public void AddComboAll(System.Windows.Forms.ComboBox combo)
        {
            this.AddComboAll(combo, "(Tất cả)");
        }

        public void AddComboAll(System.Windows.Forms.ComboBox combo, string text)
        {
            DataTable table = new DataTable();
            table = this.LookUp();
            DataRow row = table.NewRow();
            row["ID"] = "All";
            row["Name"] = text;
            table.Rows.InsertAt(row, 0);
            MyLib.TableToComboBox(combo, table, "ID", "Name");
        }

        public void AddComboDefault(System.Windows.Forms.ComboBox combo)
        {
            this.AddComboAll(combo, "(Mặc định)");
        }

        public int Count()
        {
            SqlHelper helper = new SqlHelper();
            helper.Error += new SqlHelper.ErrorEventHander(this.DispError);
            if (helper.Open() != "OK")
            {
                return 0;
            }
            int num = this.Count(helper.Connection);
            helper.Close();
            return num;
        }

        public int Count(SqlConnection myConnection)
        {
            SqlHelper helper = new SqlHelper();
            helper.Error += new SqlHelper.ErrorEventHander(this.DispError);
            return helper.ExecuteScalar(myConnection, "PRODUCT_UNIT_Count", 0);
        }

        public int Count(SqlTransaction myTransaction)
        {
            SqlHelper helper = new SqlHelper();
            helper.Error += new SqlHelper.ErrorEventHander(this.DispError);
            return helper.ExecuteScalar(myTransaction, "PRODUCT_UNIT_Count", 0);
        }

        public string CreateKey()
        {
            return this.CreateKey("");
        }

        public string CreateKey(SqlTransaction myTransaction)
        {
            return SqlHelper.GenCode(myTransaction, "");
        }

        public string CreateKey(string key)
        {
            return SqlHelper.GenCode(key);
        }

        public string CreateKey(SqlTransaction myTransaction, string key)
        {
            return SqlHelper.GenCode(myTransaction, key);
        }

        public bool Delete()
        {
            return this.Delete(this.m_Product_ID);
        }

        public bool Delete(SqlConnection myConnection)
        {
            return this.Delete(myConnection, this.m_Product_ID);
        }

        public bool Delete(SqlTransaction myTransaction)
        {
            return this.Delete(myTransaction, this.m_Product_ID);
        }

        public bool Delete(string Product_ID)
        {
            SqlHelper helper = new SqlHelper();
            helper.Error += new SqlHelper.ErrorEventHander(this.DispError);
            if ((helper.Open() == "OK") && this.Delete(helper.Connection, Product_ID))
            {
                helper.Close();
                return true;
            }
            return false;
        }

        public bool Delete(SqlConnection myConnection, string Product_ID)
        {
            string[] myParams = new string[] { "@Product_ID" };
            object[] myValues = new object[] { Product_ID };
            SqlHelper helper = new SqlHelper();
            helper.Error += new SqlHelper.ErrorEventHander(this.DispError);
            return (helper.ExecuteNonQuery(myConnection, "PRODUCT_UNIT_Delete", myParams, myValues) == "OK");
        }

        public bool Delete(SqlTransaction myTransaction, string Product_ID)
        {
            string[] myParams = new string[] { "@Product_ID" };
            object[] myValues = new object[] { Product_ID };
            SqlHelper helper = new SqlHelper();
            helper.Error += new SqlHelper.ErrorEventHander(this.DispError);
            return (helper.ExecuteNonQuery(myTransaction, "PRODUCT_UNIT_Delete", myParams, myValues) == "OK");
        }

        private void DispError(object sender, SqlHelperException e)
        {
            XtraMessageBox.Show(e.Message, "Th\x00f4ng B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

        public bool Exist()
        {
            return this.Exist(this.m_Product_ID, this.m_Unit_ID, this.m_UnitConvert_ID);
        }

        public bool Exist(SqlTransaction myTransaction)
        {
            return this.Exist(myTransaction, this.m_Product_ID, this.m_Unit_ID, this.m_UnitConvert_ID);
        }

        public bool Exist(string Product_ID, string Unit_ID, string UnitConvert_ID)
        {
            SqlHelper helper = new SqlHelper();
            helper.Error += new SqlHelper.ErrorEventHander(this.DispError);
            if ((helper.Open() == "OK") && this.Exist(helper.Connection, Product_ID, Unit_ID, UnitConvert_ID))
            {
                helper.Close();
                return true;
            }
            return false;
        }

        public bool Exist(SqlConnection myConnection, string Product_ID, string Unit_ID, string UnitConvert_ID)
        {
            bool hasRows = false;
            string[] myParams = new string[] { "@Product_ID", "@Unit_ID", "@UnitConvert_ID" };
            object[] myValues = new object[] { Product_ID, Unit_ID, UnitConvert_ID };
            SqlHelper helper = new SqlHelper();
            SqlDataReader reader = helper.ExecuteReader(myConnection, "PRODUCT_UNIT_Get", myParams, myValues);
            if (reader != null)
            {
                hasRows = reader.HasRows;
                reader.Close();
                return hasRows;
            }
            helper.Close();
            if (MyParamaters.IsShowWarning)
            {
                XtraMessageBox.Show(helper.Result, "Th\x00f4ng B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            return hasRows;
        }

        public bool Exist(SqlTransaction myTransaction, string Product_ID, string Unit_ID, string UnitConvert_ID)
        {
            bool hasRows = false;
            string[] myParams = new string[] { "@Product_ID", "@Unit_ID", "@UnitConvert_ID" };
            object[] myValues = new object[] { Product_ID, Unit_ID, UnitConvert_ID };
            SqlHelper helper = new SqlHelper();
            SqlDataReader reader = helper.ExecuteReader(myTransaction, "PRODUCT_UNIT_Get", myParams, myValues);
            if (reader != null)
            {
                hasRows = reader.HasRows;
                reader.Close();
                return hasRows;
            }
            helper.RollBack(myTransaction);
            if (MyParamaters.IsShowWarning)
            {
                XtraMessageBox.Show(helper.Result, "Th\x00f4ng B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            return hasRows;
        }

        public bool Get()
        {
            return this.Get(this.m_Product_ID, this.m_Unit_ID, this.m_UnitConvert_ID);
        }

        public bool Get(SqlConnection myConnection)
        {
            return this.Get(myConnection, this.m_Product_ID, this.m_Unit_ID, this.m_UnitConvert_ID);
        }

        public bool Get(SqlTransaction myTransaction)
        {
            return this.Get(myTransaction, this.m_Product_ID, this.m_Unit_ID, this.m_UnitConvert_ID);
        }

        public bool Get(string Product_ID, string Unit_ID, string UnitConvert_ID)
        {
            SqlHelper helper = new SqlHelper();
            helper.Error += new SqlHelper.ErrorEventHander(this.DispError);
            if ((helper.Open() == "OK") && this.Get(helper.Connection, Product_ID, Unit_ID, UnitConvert_ID))
            {
                helper.Close();
                return true;
            }
            return false;
        }

        public bool Get(SqlConnection myConnection, string Product_ID, string Unit_ID, string UnitConvert_ID)
        {
            bool flag = false;
            string[] myParams = new string[] { "@Product_ID", "@Unit_ID", "@UnitConvert_ID" };
            object[] myValues = new object[] { Product_ID, Unit_ID, UnitConvert_ID };
            SqlHelper helper = new SqlHelper();
            SqlDataReader reader = helper.ExecuteReader(myConnection, "PRODUCT_UNIT_Get", myParams, myValues);
            if (reader != null)
            {
                while (reader.Read())
                {
                    this.m_Product_ID = Convert.ToString(reader["Product_ID"]);
                    this.m_Unit_ID = Convert.ToString(reader["Unit_ID"]);
                    this.m_UnitConvert_ID = Convert.ToString(reader["UnitConvert_ID"]);
                    this.m_UnitConvert = Convert.ToDecimal(reader["UnitConvert"]);
                    this.m_Sorted = Convert.ToInt64(reader["Sorted"]);
                    flag = true;
                }
                reader.Close();
                reader = null;
                return flag;
            }
            helper.Close();
            if (MyParamaters.IsShowWarning)
            {
                XtraMessageBox.Show(helper.Result, "Th\x00f4ng B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            return flag;
        }

        public bool Get(SqlTransaction myTransaction, string Product_ID, string Unit_ID, string UnitConvert_ID)
        {
            bool flag = false;
            string[] myParams = new string[] { "@Product_ID", "@Unit_ID", "@UnitConvert_ID" };
            object[] myValues = new object[] { Product_ID, Unit_ID, UnitConvert_ID };
            SqlHelper helper = new SqlHelper();
            SqlDataReader reader = helper.ExecuteReader(myTransaction, "PRODUCT_UNIT_Get", myParams, myValues);
            if (reader != null)
            {
                while (reader.Read())
                {
                    this.m_Product_ID = Convert.ToString(reader["Product_ID"]);
                    this.m_Unit_ID = Convert.ToString(reader["Unit_ID"]);
                    this.m_UnitConvert_ID = Convert.ToString(reader["UnitConvert_ID"]);
                    this.m_UnitConvert = Convert.ToDecimal(reader["UnitConvert"]);
                    this.m_Sorted = Convert.ToInt64(reader["Sorted"]);
                    flag = true;
                }
                reader.Close();
                reader = null;
                return flag;
            }
            helper.RollBack(myTransaction);
            if (MyParamaters.IsShowWarning)
            {
                XtraMessageBox.Show(helper.Result, "Th\x00f4ng B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            return flag;
        }

        public decimal GetConvert(string ProductID, string UnitID, string UnitConvertID)
        {
            PRODUCT product = new PRODUCT();
            if (product.Get(ProductID) != "OK")
            {
                return 1M;
            }
            if (product.Unit == UnitConvertID)
            {
                return 1M;
            }
            SqlHelper helper = new SqlHelper();
            string[] myParams = new string[] { "@Product_ID", "@Unit_ID", "@UnitConvert_ID" };
            object[] myValues = new object[] { ProductID, UnitID, UnitConvertID };
            helper.CommandType = CommandType.Text;
            return helper.ExecuteScalar("Select UnitConvert From [PRODUCT_UNIT] Where  Product_ID = @Product_ID AND  Unit_ID = @Unit_ID AND  UnitConvert_ID = @UnitConvert_ID", myParams, myValues, 0);
        }

        public DataTable GetList()
        {
            SqlHelper helper = new SqlHelper();
            helper.Error += new SqlHelper.ErrorEventHander(this.DispError);
            return helper.ExecuteDataTable("PRODUCT_UNIT_GetList");
        }

        public DataTable GetList(SqlConnection myConnection)
        {
            SqlHelper helper = new SqlHelper();
            helper.Error += new SqlHelper.ErrorEventHander(this.DispError);
            return helper.ExecuteDataTable(myConnection, "PRODUCT_UNIT_GetList");
        }

        public DataTable GetList(SqlTransaction myTransaction)
        {
            SqlHelper helper = new SqlHelper();
            helper.Error += new SqlHelper.ErrorEventHander(this.DispError);
            return helper.ExecuteDataTable(myTransaction, "PRODUCT_UNIT_GetList");
        }

        public DataTable GetList(string Product_ID)
        {
            string[] myParams = new string[] { "@Product_ID" };
            object[] myValues = new object[] { Product_ID };
            SqlHelper helper = new SqlHelper();
            return helper.ExecuteDataTable("UNITCONVERT_GetList_PRODUCT", myParams, myValues);
        }

        public bool Insert()
        {
            return this.Insert(this.m_Product_ID, this.m_Unit_ID, this.m_UnitConvert_ID, this.m_UnitConvert, this.m_Sorted);
        }

        public bool Insert(SqlConnection myConnection)
        {
            return this.Insert(myConnection, this.m_Product_ID, this.m_Unit_ID, this.m_UnitConvert_ID, this.m_UnitConvert, this.m_Sorted);
        }

        public bool Insert(SqlTransaction myTransaction)
        {
            return this.Insert(myTransaction, this.m_Product_ID, this.m_Unit_ID, this.m_UnitConvert_ID, this.m_UnitConvert, this.m_Sorted);
        }

        public bool Insert(string Product_ID, string Unit_ID, string UnitConvert_ID, decimal UnitConvert, long Sorted)
        {
            SqlHelper helper = new SqlHelper();
            helper.Error += new SqlHelper.ErrorEventHander(this.DispError);
            if ((helper.Open() == "OK") && this.Insert(helper.Connection, Product_ID, Unit_ID, UnitConvert_ID, UnitConvert, Sorted))
            {
                helper.Close();
                return true;
            }
            return false;
        }

        public bool Insert(SqlConnection myConnection, string Product_ID, string Unit_ID, string UnitConvert_ID, decimal UnitConvert, long Sorted)
        {
            string[] myParams = new string[] { "@Product_ID", "@Unit_ID", "@UnitConvert_ID", "@UnitConvert", "@Sorted" };
            object[] myValues = new object[] { Product_ID, Unit_ID, UnitConvert_ID, UnitConvert, Sorted };
            SqlHelper helper = new SqlHelper();
            helper.Error += new SqlHelper.ErrorEventHander(this.DispError);
            return (helper.ExecuteNonQuery(myConnection, "PRODUCT_UNIT_Insert", myParams, myValues) == "OK");
        }

        public bool Insert(SqlTransaction myTransaction, string Product_ID, string Unit_ID, string UnitConvert_ID, decimal UnitConvert, long Sorted)
        {
            string[] myParams = new string[] { "@Product_ID", "@Unit_ID", "@UnitConvert_ID", "@UnitConvert", "@Sorted" };
            object[] myValues = new object[] { Product_ID, Unit_ID, UnitConvert_ID, UnitConvert, Sorted };
            SqlHelper helper = new SqlHelper();
            helper.Error += new SqlHelper.ErrorEventHander(this.DispError);
            return (helper.ExecuteNonQuery(myTransaction, "PRODUCT_UNIT_Insert", myParams, myValues) == "OK");
        }

        public DataTable LookUp()
        {
            SqlHelper helper = new SqlHelper();
            helper.Error += new SqlHelper.ErrorEventHander(this.DispError);
            return helper.ExecuteDataTable("PRODUCT_UNIT_LookUp");
        }

        public string NewID()
        {
            return this.NewID("");
        }

        public string NewID(string key)
        {
            return SqlHelper.GenCode("PRODUCT_UNIT", "PRODUCT_UNITID", key);
        }

        public string NewID(SqlTransaction myTransaction, string key)
        {
            return SqlHelper.GenCode(myTransaction, "PRODUCT_UNIT", "PRODUCT_UNITID", key);
        }

        public DataTable Search(decimal UnitConvert)
        {
            SqlHelper helper = new SqlHelper();
            helper.Error += new SqlHelper.ErrorEventHander(this.DispError);
            if (helper.Open() != "OK")
            {
                return null;
            }
            return this.Search(helper.Connection, UnitConvert);
        }

        public DataTable Search(SqlConnection myConnection, decimal UnitConvert)
        {
            string[] myParams = new string[] { "@UnitConvert" };
            object[] myValues = new object[] { "%" + UnitConvert + "%" };
            SqlHelper helper = new SqlHelper();
            helper.Error += new SqlHelper.ErrorEventHander(this.DispError);
            return helper.ExecuteDataTable(myConnection, "PRODUCT_UNIT_Search", myParams, myValues);
        }

        public DataTable Search(SqlTransaction myTransaction, decimal UnitConvert)
        {
            string[] myParams = new string[] { "@UnitConvert" };
            object[] myValues = new object[] { "%" + UnitConvert + "%" };
            SqlHelper helper = new SqlHelper();
            helper.Error += new SqlHelper.ErrorEventHander(this.DispError);
            return helper.ExecuteDataTable(myTransaction, "PRODUCT_UNIT_Search", myParams, myValues);
        }

        public void SetData(string Product_ID, string Unit_ID, string UnitConvert_ID, decimal UnitConvert, long Sorted)
        {
            this.m_Product_ID = Product_ID;
            this.m_Unit_ID = Unit_ID;
            this.m_UnitConvert_ID = UnitConvert_ID;
            this.m_UnitConvert = UnitConvert;
            this.m_Sorted = Sorted;
        }

        public bool Update()
        {
            return this.Update(this.m_Product_ID, this.m_Unit_ID, this.m_UnitConvert_ID, this.m_UnitConvert, this.m_Sorted);
        }

        public bool Update(SqlConnection myConnection)
        {
            return this.Update(this.m_Product_ID, this.m_Unit_ID, this.m_UnitConvert_ID, this.m_UnitConvert, this.m_Sorted);
        }

        public bool Update(SqlTransaction myTransaction)
        {
            return this.Update(myTransaction, this.m_Product_ID, this.m_Unit_ID, this.m_UnitConvert_ID, this.m_UnitConvert, this.m_Sorted);
        }

        public bool Update(string Product_ID, string Unit_ID, string UnitConvert_ID, decimal UnitConvert, long Sorted)
        {
            SqlHelper helper = new SqlHelper();
            helper.Error += new SqlHelper.ErrorEventHander(this.DispError);
            if ((helper.Open() == "OK") && this.Update(helper.Connection, Product_ID, Unit_ID, UnitConvert_ID, UnitConvert, Sorted))
            {
                helper.Close();
                return true;
            }
            return false;
        }

        public bool Update(SqlConnection myConnection, string Product_ID, string Unit_ID, string UnitConvert_ID, decimal UnitConvert, long Sorted)
        {
            string[] myParams = new string[] { "@Product_ID", "@Unit_ID", "@UnitConvert_ID", "@UnitConvert", "@Sorted" };
            object[] myValues = new object[] { Product_ID, Unit_ID, UnitConvert_ID, UnitConvert, Sorted };
            SqlHelper helper = new SqlHelper();
            helper.Error += new SqlHelper.ErrorEventHander(this.DispError);
            return (helper.ExecuteNonQuery(myConnection, "PRODUCT_UNIT_Update", myParams, myValues) == "OK");
        }

        public bool Update(SqlTransaction myTransaction, string Product_ID, string Unit_ID, string UnitConvert_ID, decimal UnitConvert, long Sorted)
        {
            string[] myParams = new string[] { "@Product_ID", "@Unit_ID", "@UnitConvert_ID", "@UnitConvert", "@Sorted" };
            object[] myValues = new object[] { Product_ID, Unit_ID, UnitConvert_ID, UnitConvert, Sorted };
            SqlHelper helper = new SqlHelper();
            helper.Error += new SqlHelper.ErrorEventHander(this.DispError);
            return (helper.ExecuteNonQuery(myTransaction, "PRODUCT_UNIT_Update", myParams, myValues) == "OK");
        }

        public bool UpdateByID()
        {
            return this.UpdateByID(this.m_Product_ID, this.m_Unit_ID, this.m_UnitConvert_ID, this.m_UnitConvert);
        }

        public bool UpdateByID(SqlConnection myConnection)
        {
            return this.UpdateByID(myConnection, this.m_Product_ID, this.m_Unit_ID, this.m_UnitConvert_ID, this.m_UnitConvert);
        }

        public bool UpdateByID(SqlTransaction myTransaction)
        {
            return this.UpdateByID(myTransaction, this.m_Product_ID, this.m_Unit_ID, this.m_UnitConvert_ID, this.m_UnitConvert);
        }

        public bool UpdateByID(string Product_ID, string Unit_ID, string UnitConvert_ID, decimal UnitConvert)
        {
            SqlHelper helper = new SqlHelper();
            helper.Error += new SqlHelper.ErrorEventHander(this.DispError);
            if ((helper.Open() == "OK") && this.UpdateByID(helper.Connection, Product_ID, Unit_ID, UnitConvert_ID, UnitConvert))
            {
                helper.Close();
                return true;
            }
            return false;
        }

        public bool UpdateByID(SqlConnection myConnection, string Product_ID, string Unit_ID, string UnitConvert_ID, decimal UnitConvert)
        {
            string[] myParams = new string[] { "@Product_ID", "@Unit_ID", "@UnitConvert_ID", "@UnitConvert" };
            object[] myValues = new object[] { Product_ID, Unit_ID, UnitConvert_ID, UnitConvert };
            SqlHelper helper = new SqlHelper();
            helper.Error += new SqlHelper.ErrorEventHander(this.DispError);
            return (helper.ExecuteNonQuery(myConnection, "PRODUCT_UNIT_UpdateByID", myParams, myValues) == "OK");
        }

        public bool UpdateByID(SqlTransaction myTransaction, string Product_ID, string Unit_ID, string UnitConvert_ID, decimal UnitConvert)
        {
            string[] myParams = new string[] { "@Product_ID", "@Unit_ID", "@UnitConvert_ID", "@UnitConvert" };
            object[] myValues = new object[] { Product_ID, Unit_ID, UnitConvert_ID, UnitConvert };
            SqlHelper helper = new SqlHelper();
            helper.Error += new SqlHelper.ErrorEventHander(this.DispError);
            return (helper.ExecuteNonQuery(myTransaction, "PRODUCT_UNIT_UpdateByID", myParams, myValues) == "OK");
        }

        public string AssemblyVersion
        {
            get
            {
                return "2.5.0.0";
            }
        }

        public string Copyright
        {
            get
            {
                return "C\x00f4ng Ty Phần Mềm Ho\x00e0n Hảo";
            }
        }

        public string Product
        {
            get
            {
                return "Class PRODUCT_UNIT";
            }
        }

        [Category("Primary Key"), DisplayName("Product_ID")]
        public string Product_ID
        {
            get
            {
                return this.m_Product_ID;
            }
            set
            {
                this.m_Product_ID = value;
            }
        }

        [Category("Column"), DisplayName("Sorted")]
        public long Sorted
        {
            get
            {
                return this.m_Sorted;
            }
            set
            {
                this.m_Sorted = value;
            }
        }

        [Category("Primary Key"), DisplayName("Unit_ID")]
        public string Unit_ID
        {
            get
            {
                return this.m_Unit_ID;
            }
            set
            {
                this.m_Unit_ID = value;
            }
        }

        [Category("Column"), DisplayName("UnitConvert")]
        public decimal UnitConvert
        {
            get
            {
                return this.m_UnitConvert;
            }
            set
            {
                this.m_UnitConvert = value;
            }
        }

        [DisplayName("UnitConvert_ID"), Category("Primary Key")]
        public string UnitConvert_ID
        {
            get
            {
                return this.m_UnitConvert_ID;
            }
            set
            {
                this.m_UnitConvert_ID = value;
            }
        }
    }
}

